using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace msgExchange
{
    public class CRegistryWrapper
    {
        private RegistryKey vRootKey_=null;
        public enum RootKeys
        {
            CLASSES_ROOT,
            CURRENT_USER,
            LOCAL_MACHINE,
            USERS,
            CURRENT_CONFIG,
            DYN_DATA,
            PERFORMANCE_DATA
        };

        public CRegistryWrapper(RootKeys RootKey)
        {
            switch (RootKey)
            {
                case RootKeys.CLASSES_ROOT:
                    vRootKey_ = Registry.ClassesRoot;
                    break;
                case RootKeys.CURRENT_USER:
                    vRootKey_ = Registry.CurrentUser;
                    break;
                case RootKeys.LOCAL_MACHINE:
                    vRootKey_ = Registry.LocalMachine;
                    break;
                case RootKeys.USERS:
                    vRootKey_ = Registry.Users;
                    break;
                case RootKeys.CURRENT_CONFIG:
                    vRootKey_ = Registry.CurrentConfig;
                    break;
                case RootKeys.DYN_DATA:
                    vRootKey_ = Registry.DynData;
                    break;
                case RootKeys.PERFORMANCE_DATA:
                    vRootKey_ = Registry.PerformanceData; 
                    break;
                default:
                    vRootKey_ = Registry.PerformanceData;
                    break;
            }
        }



        public string GetRegVal(string keypath, string keyname)
        {
            try
            {
                RegistryKey key = vRootKey_.OpenSubKey(keypath);
                return key.GetValue(keyname, (object)"").ToString();
            }
            catch
            {
                return "";
            }
        }


        public bool SetRegVal(string keypath, string keyname, string keyval)
        {
            try
            {
                RegistryKey key = vRootKey_.OpenSubKey(keypath, true);
                key.SetValue(keyname, (object)keyval);
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool CreateRegKey(string keypath)
        {
            try
            {
                return (null != vRootKey_.CreateSubKey(keypath,RegistryKeyPermissionCheck.ReadWriteSubTree));
            }
            catch
            {
                return false;
            }
        }
 


        public bool DelRegSubKey(string keypath)
        {
            try
            {
                vRootKey_.DeleteSubKeyTree(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool DelRegKeyVal(string keypath, string keyname)
        {
            try
            {
                RegistryKey key = vRootKey_.OpenSubKey(keypath, true);
                key.DeleteValue(keyname);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}






